﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace newTurtle8
{
    public partial class ZimunimList : Form
    {
        public enum TableOption { AlreadyCalled, NotCalledYet }
        DataTable dt;
        Mofa mofa;
        SqlCommand selectCmd;

        public ZimunimList(Mofa mofa, IList<string> armyIdList, TableOption option)
        {
            InitializeComponent();

            this.mofa = mofa;

            if (option == TableOption.NotCalledYet)
                selectCmd = SqlManager.getSqlCommandFromFile(@"\SELECT\AllActiveSoldiers.SELECT.sql");
            else
            {
                selectCmd = SqlManager.getSqlCommandFromFile(@"\SELECT\AlreadyCalledSoldiers.SELECT.sql");
                selectCmd.CommandText = selectCmd.CommandText.Replace("[Table]", mofa.TableName);
                selectCmd.Parameters.Add("@startDate", SqlDbType.Date).Value = mofa.StartDate;
            }
            dt = SqlManager.selectSqlQueryDataTable(selectCmd);
            dataGridView.DataSource = dt;

            // i want to create a list of all not-called-soldiers
            if (option == TableOption.NotCalledYet)
            {
                Stack<int> remove_stack = new Stack<int>();

                //creates a stack with row numbers, containing armyID's of soldiers already called.
                foreach (DataRow row in dt.Rows)
                    if (armyIdList.Contains((string)row["armyID"]))
                        remove_stack.Push(dt.Rows.IndexOf(row));

                while (remove_stack.Count > 0)
                    dt.Rows.RemoveAt(remove_stack.Pop());
            }
           
            Translator.setTableColumnHeadersHebrew(dataGridView.Columns);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            List<string> addList = new List<string>();
            foreach (DataGridViewRow row in dataGridView.SelectedRows)
                addList.Add((string)row.Cells["armyID"].Value);

           //mofa.addSoldiers(addList);
            DialogResult = DialogResult.OK;

            this.Dispose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
    }
}

